package org.yxj.customlibrary.v;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;


import org.yxj.customlibrary.R;

import java.util.ArrayList;
import java.util.List;

public class PainColorListView extends RecyclerView {

    private BaseQuickAdapter adapter;

    private int checkColor;

    public List<Integer> getColorList() {
        List<Integer> list = new ArrayList<>();
        list.add(getResources().getColor(R.color.black));
        list.add(getResources().getColor(R.color.red));
        list.add(getResources().getColor(R.color.colorPrimaryDark));
        list.add(getResources().getColor(R.color.end_color));
        list.add(getResources().getColor(R.color.indianred));
        list.add(getResources().getColor(R.color.mediumvioletred));
        list.add(getResources().getColor(R.color.orange));
        return list;
    }


    public PainColorListView(@NonNull Context context) {
        this(context, null);
    }


    public PainColorListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public PainColorListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addItemDecoration(new MyItem(7));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(HORIZONTAL);
        setLayoutManager(linearLayoutManager);
        setAdapter(adapter = new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.item_paint_color_list_layout,
                getColorList()) {
            @Override
            protected void convert(BaseViewHolder helper, Integer item) {
                if (checkColor == item) {
                    helper.getView(R.id.paintColor).setBackground(getBG(item, 10));
                    setSize(helper.getView(R.id.paintColor), dp2Px(20));
                } else {
                    helper.getView(R.id.paintColor).setBackground(getBG(item, 3));
                    setSize(helper.getView(R.id.paintColor), dp2Px(20));
                }
            }
        });


        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Integer item = getColorList().get(position);
                checkColor = item;
                adapter.notifyDataSetChanged();

                if (listener != null) {
                    listener.onResult(checkColor);
                }
            }
        });
    }


    public int dp2Px(int dp) {
        return (int) (getResources().getDisplayMetrics().density * dp + 0.5f);
    }

    public GradientDrawable getBG(int color, int strokeWidth) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(1000);
        gradientDrawable.setColor(color);
        gradientDrawable.setStroke(strokeWidth, Color.WHITE);
        return gradientDrawable;
    }


    private void setSize(View view, int size) {
        ViewGroup.LayoutParams param = view.getLayoutParams();
        param.width = param.height = size;
        view.setLayoutParams(param);
    }

    private ResultListener listener;

    public void setListener(ResultListener listener) {
        this.listener = listener;
        if (listener != null) {
            listener.onResult(checkColor = getColorList().get(0));
            adapter.notifyDataSetChanged();
        }
    }

    public interface ResultListener {
        void onResult(int color);
    }


    class MyItem extends ItemDecoration {

        private int span = dp2Px(2);
        public MyItem(int span) {
            this.span = dp2Px(span);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(span, span, span, span);
        }
    }
}

